﻿using BUGCOME.Common;
using BUGCOME.Infrastructure.WebExtensions;
using BUGCOME.Model.Auth;
using BUGCOME.ServiceCore.Filters;
using Mapster;
using Microsoft.AspNetCore.Mvc;

namespace ZS.Admin.WebApi.Controllers.System
{
    /// <summary>
    /// 数据字典类型控制器，处理数据字典类型相关API请求
    /// </summary>
    [Verify]
    [Route("commonapi/[controller]/[action]")]
    [ApiExplorerSettings(GroupName = "common")]
    public class DictTypeController : BaseController
    {
        /// <summary>数据字典类型服务接口实例</summary>
        private readonly IDictTypeService dictTypeService;

        /// <summary>构造函数，依赖注入数据字典类型服务</summary>
        /// <param name="dictTypeService">数据字典类型服务实例</param>
        public DictTypeController(IDictTypeService dictTypeService)
        {
            this.dictTypeService = dictTypeService;
        }

        /// <summary>
        /// 分页查询字典类型
        /// </summary>
        /// <param name="dict">查询条件</param>
        /// <param name="pagerInfo">分页信息</param>
        /// <returns>分页列表</returns>
        [ActionPermissionFilter(Permission = "admin:dict:list")]
        [HttpPost]
        public async Task<IActionResult> List([FromBody] DictTypeQueryDto dict)
        {

            var list = await dictTypeService.SelectDictTypeListAsync(dict);
            return Success(list, TimeFormatFull);
        }

        /// <summary>
        /// 查询字典类型详细
        /// </summary>
        /// <param name="dto">dto.value字典ID</param>
        /// <returns>字典类型详情</returns>
        [HttpPost]
        [ActionPermissionFilter(Permission = "admin:dict:query")]
        public async Task<IActionResult> GetInfo([FromBody]ParmDto<long> dto)
        {
            var info = await dictTypeService.GetInfoAsync(dto.Value);
            return Success(info);
        }

        /// <summary>
        /// 添加字典类型
        /// </summary>
        /// <param name="dto">字典类型DTO</param>
        /// <returns>操作结果</returns>
        [ActionPermissionFilter(Permission = "admin:dict:add")]
        [Log(Title = "字典操作", BusinessType = BusinessType.INSERT)]
        [HttpPost]
        public async Task<IActionResult> Add([FromBody] DictTypeDto dto)
        {
            DictType dict = dto.Adapt<DictType>().ToCreate(HttpContext);
            // 异步检查字典类型唯一性
            var uniqueResult = await dictTypeService.CheckDictTypeUniqueAsync(dict);
            if (UserConstants.NOT_UNIQUE.Equals(uniqueResult))
            {
                return ToResponse(ApiResult.Error($"新增字典'{dict.DictName}'失败，字典类型已存在"));
            }
            // 异步插入字典类型
            var insertResult = await dictTypeService.InsertDictTypeAsync(dict);
            return Success(insertResult);
        }

        /// <summary>
        /// 修改字典类型
        /// </summary>
        /// <param name="dto">字典类型DTO</param>
        /// <returns>操作结果</returns>
        [ActionPermissionFilter(Permission = "admin:dict:edit")]
        [Log(Title = "字典操作", BusinessType = BusinessType.UPDATE)]
        [HttpPost]
        [HttpPut]
        public async Task<IActionResult> Edit([FromBody] DictTypeDto dto)
        {
            DictType dict = dto.Adapt<DictType>();
            // 异步检查字典类型唯一性
            var uniqueResult = await dictTypeService.CheckDictTypeUniqueAsync(dict);
            if (UserConstants.NOT_UNIQUE.Equals(uniqueResult))
            {
                return ToResponse(ApiResult.Error($"修改字典'{dict.DictName}'失败，字典类型已存在"));
            }
            dict.UpdateBy = HttpContext.GetName();
            // 异步更新字典类型
            var updateResult = await dictTypeService.UpdateDictTypeAsync(dict);
            return Success(updateResult);
        }

        /// <summary>
        /// 删除字典类型
        /// </summary>
        /// <param name="dto">dto.value字典ID集合（逗号分隔）</param>
        /// <returns>操作结果</returns>
        [ActionPermissionFilter(Permission = "admin:dict:remove")]
        [Log(Title = "删除字典类型", BusinessType = BusinessType.DELETE)]
        [HttpPost]
        public async Task<IActionResult> Remove([FromBody]ParmDto<string> dto)
        {
            long[] ids = Tools.SpitLongArrary(dto.Value);
            // 异步删除字典类型
            var deleteResult = await dictTypeService.DeleteDictTypeByIdsAsync(ids);
            return Success(deleteResult);
        }
    }
}